import 'dart:math';

import 'package:flame/components.dart';
import 'package:flame/extensions.dart';
import 'package:flutter/material.dart';

class Ground extends Component {
  static const double size = 1500;
  static const Rect bounds = Rect.fromLTWH(-size, -size, 2 * size, 2 * size);
  static final Paint paintBorder = Paint()
    ..color = Colors.white
    ..strokeWidth = 10
    ..style = PaintingStyle.stroke;
  static final Paint paintBg = Paint()..color = const Color(0xFF333333);
  static final rng = Random();
  late final List<Paint> paintPool;
  late final List<Rect> rectPool;

  Ground() : super(priority: 0) {
    paintPool = List<Paint>.generate(
      (size / 50).ceil(),
      (index) => PaintExtension.random(rng: rng)
        ..style = PaintingStyle.stroke
        ..strokeWidth = 2,
      growable: false,
    );
  }

  @override
  void render(Canvas canvas) {
    canvas.drawRect(bounds, paintBg);
    canvas.drawRect(bounds, paintBorder);
    for (var i = 0; i < (size / 50).ceil(); i++) {
      canvas.drawCircle(Offset.zero, size - i * 50, paintPool[i]);
      canvas.drawRect(rectPool[i], paintBorder);
    }
  }

  static double genCoord() {
    return -size + rng.nextDouble() * (2 * size);
  }
}
